pkgname=lkp-tests
pkgver=0
pkgrel=1
arch=('i386' 'x86_64')
url="https://gitee.com/compass-ci/lkp-tests.git"
source=("https://gitee.com/compass-ci/lkp-tests.git")
license=('GPL2.0')

LKP_SRC=${LKP_SRC:-/lkp/$USER/src}
LKP_USER=${LKP_USER:-lkp}

build()
{
	ARCH=$CARCH make -C $LKP_SRC/bin/event || {
		local ret=$?

		[[ $CARCH = 'i386' ]] && {
			local gcc_ver=$(gcc -dumpversion)
			echo Try: apt-get install lib32gcc-${gcc_ver%%.*}-dev
		}

		return $ret
	}
}

package()
{
	umask 002
	tmp_dir=/tmp/lkp_initrd-$USER
	rm -fr "$tmp_dir"
	mkdir -p $tmp_dir/rootfs/lkp/$LKP_USER/src
	mkdir -p $tmp_dir/lkp/$LKP_USER
	ln -sfT $LKP_SRC $tmp_dir/lkp/$LKP_USER/src

	rm -rf "${pkgdir}"

	mkdir -p "${pkgdir}/lkp/$LKP_USER/src"
	rsync -a $tmp_dir/rootfs/lkp "${pkgdir}"
	rsync -a $tmp_dir/lkp/$LKP_USER/src/* "${pkgdir}/lkp/$LKP_USER/src"

	{
		cd $tmp_dir/lkp/$LKP_USER/src/rootfs/addon

		[ -d etc/ssh ] &&
		chmod -R 600 etc/ssh/* # to avoid sshd Permissions too open error
		chmod g-ws root
		[ -d root/.ssh ] && {
			chmod -R go-rwxs root/.ssh
			mkdir -p etc/dropbear
			cp -alf root/.ssh/authorized_keys etc/dropbear/
		}

		# don't override /lib, it could be a soft link to usr/lib at some distro(clear is known)
		# don't override /etc/init.d, it is a soft link to /etc/rc.d/init.d on some distributions(e.g. centos, aliyun)

		rsync -au . "${pkgdir}"
	}

	{
		# restore permission for cleanup
		cd $tmp_dir/lkp/$LKP_USER/src/rootfs/addon
		find . -type d -exec chmod g+rwx \{\} \;
		chmod -R g+rw *
	}

	[ -d "/osimage/addon-${CARCH}" ] && {
		cd /osimage/addon-${CARCH}
		rsync -a . "${pkgdir}"
	}
	return 0
}
